"""
权限系统数据库迁移示例
使用Alembic创建权限系统相关的数据库表
"""
# 这是一个示例文件，展示如何使用Alembic创建权限系统的数据库表
# 实际使用时，请运行以下命令生成迁移文件：
# alembic revision --autogenerate -m "Add permission system tables"

"""
# 在alembic/versions/目录下会生成类似以下的迁移文件：

def upgrade():
    # 创建角色表
    op.create_table('role',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=50), nullable=False),
        sa.Column('code', sa.String(length=50), nullable=False),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=False),
        sa.Column('create_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('update_time', sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('code'),
        sa.UniqueConstraint('name')
    )
    op.create_index(op.f('ix_role_code'), 'role', ['code'], unique=True)
    op.create_index(op.f('ix_role_name'), 'role', ['name'], unique=True)

    # 创建权限表
    op.create_table('permission',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=50), nullable=False),
        sa.Column('code', sa.String(length=100), nullable=False),
        sa.Column('resource', sa.String(length=50), nullable=False),
        sa.Column('action', sa.String(length=20), nullable=False),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=False),
        sa.Column('create_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('update_time', sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('code'),
        sa.UniqueConstraint('name')
    )
    op.create_index(op.f('ix_permission_code'), 'permission', ['code'], unique=True)
    op.create_index(op.f('ix_permission_name'), 'permission', ['name'], unique=True)

    # 创建用户角色关联表
    op.create_table('user_role',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('role_id', sa.Integer(), nullable=False),
        sa.Column('create_time', sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(['role_id'], ['role.id'], ),
        sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
        sa.PrimaryKeyConstraint('id')
    )

    # 创建角色权限关联表
    op.create_table('role_permission',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('role_id', sa.Integer(), nullable=False),
        sa.Column('permission_id', sa.Integer(), nullable=False),
        sa.Column('create_time', sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(['permission_id'], ['permission.id'], ),
        sa.ForeignKeyConstraint(['role_id'], ['role.id'], ),
        sa.PrimaryKeyConstraint('id')
    )

def downgrade():
    # 删除表
    op.drop_table('role_permission')
    op.drop_table('user_role')
    op.drop_table('permission')
    op.drop_table('role')
"""

# 生成迁移文件的命令：
# alembic revision --autogenerate -m "Add permission system tables"
# alembic upgrade head
